package com.example.muyonghui.mytimetable;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CourseDetailActivity extends AppCompatActivity {

    private MyDatabaseHelper dbHelper;

    private TextView courseName;

    private TextView courseTeacher;

    private EditText courseTime;

    private EditText courseAddress;

    private Button delete;

    private Button save;

    private String coursename;

    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail);
        Intent intent = getIntent();
        coursename = intent.getStringExtra("current_course");


        courseName = (TextView) findViewById(R.id.show_course_name);
        courseTeacher = (TextView) findViewById(R.id.show_course_teacher);
        courseTime = (EditText) findViewById(R.id.show_course_time);
        courseAddress = (EditText) findViewById(R.id.show_course_address);
        delete = (Button) findViewById(R.id.button_del);
        save = (Button) findViewById(R.id.button_save);

        dbHelper = new MyDatabaseHelper(this, "UserStore.db", null, 3);

        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        username = pref.getString("username", "");
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("Course", null, "username like '"+username+"' and course_name like '"+coursename+"'", null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                // 遍历Cursor对象，取出数据并打印
                courseName.setText(cursor.getString(cursor. getColumnIndex("course_name")));
                courseTeacher.setText(cursor.getString(cursor. getColumnIndex("course_teacher")));
                courseTime.setText(cursor.getString(cursor. getColumnIndex("course_time")));
                courseAddress.setText(cursor.getString(cursor. getColumnIndex("course_address")));
            } while (cursor.moveToNext());
        }
        cursor.close();


        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                delete();

                Toast.makeText(CourseDetailActivity.this, "已删除",
                        Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(CourseDetailActivity.this, MainActivity.class);
                CourseDetailActivity.this.startActivity(intent);
                finish();
            }
        });


        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                save();

                Toast.makeText(CourseDetailActivity.this, "保存成功",
                        Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(CourseDetailActivity.this, MainActivity.class);
                CourseDetailActivity.this.startActivity(intent);
                finish();
            }
        });
    }

    public void delete() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete("Course", "course_name = ? and username = ?", new String[]{coursename,username});
    }


    public void save() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("course_address", courseAddress.getText().toString());
        cv.put("course_time", courseTime.getText().toString());
        db.update("Course", cv, "course_name = ? and username = ?", new String[]{coursename,username});
    }
}
